package com.ilovepostcard;


import java.util.ArrayList;

import com.ilovepostcard.controller.PostcardController;
import com.ilovepostcard.controller.ShareController;
import com.ilovepostcard.data.Postcard;
import com.ilovepostcard.data.SendTaskInfo;
import com.ilovepostcard.model.Address;
import com.ilovepostcard.utils.DrawableManager;
import com.ilovepostcard.utils.Utils;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class PrintAddPostcardActivity extends BaseActivity implements OnClickListener{

	GridView mPCList;
	Button mConfirm;
	ImageView mReturn;
	
	TextView tip;
	@Override
    public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_print_addpostcard);
		init();
	}
	
	private void init()
	{
		mPCList = (GridView)findViewById(R.id.add_list);
		mPCList.setAdapter(new AddPostCardAdapter());
		mPCList.setVerticalSpacing(10);
		mPCList.setOnItemClickListener(new OnItemClickListener()
		{

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int index,
					long arg3) {

				Intent nextPage = new Intent(PrintAddPostcardActivity.this,SelectPostCardActivity.class);
				PrintAddPostcardActivity.this.startActivityForResult(nextPage, 1);
				
			}
			
		});
		
		mConfirm = (Button)findViewById(R.id.confirm);
		mConfirm.setOnClickListener(this);
		
		mReturn = (ImageView)findViewById(R.id.title_return);
		mReturn.setOnClickListener(this);
		
		
		tip = (TextView)findViewById(R.id.add_tip);
		Postcard currentPC = PostcardController.getInstance().getCurrentPostCard();
		AddPostCardAdapter adpater = (AddPostCardAdapter)mPCList.getAdapter();
		adpater.add(currentPC);
		adpater.notifyDataSetChanged();
		updateMessage();
	}
	
	@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		if(resultCode != RESULT_OK)
		{
			//Toast.makeText(this, R.string.error_selectImage, Toast.LENGTH_SHORT).show();
			return;
		}
		
		if(requestCode == 1)
		{

			ArrayList<com.ilovepostcard.model.Postcard> list = null;
			list= (ArrayList<com.ilovepostcard.model.Postcard>)data.getSerializableExtra("postcard");
			
			
			
			if(list!= null)
			{
				AddPostCardAdapter adpater = (AddPostCardAdapter)mPCList.getAdapter();
				for(int i = 0; i<list.size();i++)
				{
					if(list.get(i) != null)
					{
						Postcard myPc = new Postcard();
						myPc.id = list.get(i).PostcardID;
						myPc.image =Utils.drawableToBitmap(DrawableManager.getInstance().getCachedDrawable(list.get(i).ThumImgUrl));
						
						//myPc.setTmpEditPC(Utils.drawableToBitmap(DrawableManager.getInstance().getCachedDrawable(list.get(i).ThumImgUrl)));
						adpater.add(myPc);
						
					}
				}
				adpater.notifyDataSetChanged();
				
			}
			
		}
		
		updateMessage();
		
	}
	
	private void updateMessage()
	{
		AddPostCardAdapter adpater = (AddPostCardAdapter)mPCList.getAdapter();
		int count  =  adpater.getAvalidCount();
		String message =  this.getResources().getString(R.string.add_postcard_tip);
		String addMessage = String.format(message, count,15-count); 
		
		tip.setText(addMessage);
	}
	
	
	public class AddPostCardAdapter extends BaseAdapter
	{
		Postcard[] pcList = new Postcard[15];
		
		public void add(Postcard pc)
		{
			for(int i =0;i<15;i++)
			{
				if(pcList[i]==null)
				{
					pcList[i] = pc;
					break;
				}
				
			}
			
		}
		
		
		@Override
		public int getCount() {
			return 15;
		}
		
		public int getAvalidCount()
		{
			int count = 0;
			
			for(int i =0;i<pcList.length;i++)
			{
				
				if(pcList[i]!= null)
					count ++;
				
			}
			
			return count;
			
		}

		@Override
		public Object getItem(int index) {
			return pcList[index];
		}

		@Override
		public long getItemId(int index) {
			return index;
		}

		@Override
		public View getView(int position, View view, ViewGroup parent) {

			if(view == null)
			{
				view = new ImageView(PrintAddPostcardActivity.this);
				((ImageView)view).setScaleType(ImageView.ScaleType.FIT_CENTER);
				view.setLayoutParams(new GridView.LayoutParams(121, 85));
				((ImageView)view).setPadding(5, 5, 5, 5);
				view.setBackgroundResource(R.drawable.print_suit_addcard_cardbg);
			}
			
			
			
			if(pcList[position] != null)
			{
				Bitmap image = null;
				
				if(position!=0)
					image = pcList[position].image;
				else
					image = pcList[position].getTmpEditPC();
				
				if(image!= null)
					((ImageView)view).setBackgroundDrawable(new BitmapDrawable(image));//setImageBitmap(image);
				else
					((ImageView)view).setBackgroundResource(R.drawable.discover_card_loading);//setImageResource(R.drawable.discover_card_loading);
			}
			else
			{
				
				((ImageView)view).setBackgroundResource(R.drawable.print_suit_addcard_addicon);
			}
			

			return view;
		}
		
	}


	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.confirm)
		{
			ArrayList<Postcard> list = new ArrayList<Postcard>();
			AddPostCardAdapter adapter = (AddPostCardAdapter)mPCList.getAdapter();
			for (int i = 0; i < adapter.getCount(); i++) {
				Postcard pc= (Postcard)adapter.getItem(i);
				if(pc!= null)
				{
					list.add(pc);
				}
			}
			
			if(list.size()!= 15)
			{
				Toast.makeText(this, R.string.suit_tip, Toast.LENGTH_SHORT).show();
				return;
			}
			SendTaskInfo task = ShareController.getInstance().getCurrentSendTask();
			task.postcardList = list;
			
			Intent nextPage = new Intent(PrintAddPostcardActivity.this,PrintConfirmComment2Activity.class);
			PrintAddPostcardActivity.this.startActivity(nextPage);
		}
		else if(v.getId() == R.id.title_return)
		{
			finish();
		}
		
	}
}
